package bepviet.android.ui;

import java.util.ArrayList;

import bepviet.android.engine.DataAdapter;
import bepviet.android.engine.MyArrayAdapter;
import bepviet.android.engine.R;
import bepviet.android.properties.StringInPair;
import android.app.Activity;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ListNguyenLieu{
	
	
	private Activity mcontext;
	public ListNguyenLieu(Activity context){
		this.mcontext = context;
	}
	private LinearLayout layoutmain;
	private DataAdapter dataAdapter;
	private ArrayList<StringInPair> array_list;
	private ArrayList<StringInPair> array_sort;
	private MyArrayAdapter arrayadapter;
	private ListView listview;
	private AutoCompleteTextView edittext;
	int textlength=0;
	
	
	public View getView(){
		View view = LayoutInflater.from(mcontext).inflate(R.layout.list_view, null);
	   	listview = (ListView)view.findViewById(R.id.list);
		   
		   	edittext =(AutoCompleteTextView)view.findViewById(R.id.txtedit);
		   	layoutmain = (LinearLayout)mcontext.findViewById(R.id.ln);
		   	array_list = new  ArrayList<StringInPair>(); 
		   	array_sort = new ArrayList<StringInPair>();
	        dataAdapter = new DataAdapter(mcontext);	      
	        dataAdapter.OpenDataBase();	// open conect database       
	        try{
	        	Cursor c = dataAdapter.QueryData("Select * from NguyenLieu");
	        	if(c != null){
	        		
	        		if(c.moveToFirst()){
	        		
	        			do{
	        				String str = c.getString(c.getColumnIndex("TenNguyenLieu"));  
	        				int i = Integer.parseInt( c.getString(c.getColumnIndex("MaNguyenLieu"))); 	        				
	        				array_list.add(new StringInPair(str, i));
	        			}while(c.moveToNext());
	        		}
	        		
	        	}
	        }catch(Exception ex){
	        	Log.d("TAG", "Query Error");
	        }
	        dataAdapter.close(); // close conect database
	        arrayadapter = new MyArrayAdapter(mcontext, array_list,"NguyenLieu");
	        listview.setAdapter(arrayadapter);
	        
	       
 //=======================================================================================   
	        
	        edittext.addTextChangedListener(new TextWatcher() {
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {					
					array_sort.clear();
					textlength = edittext.getText().length();
					String str = edittext.getText().toString();
					for(StringInPair item : array_list){
						if(textlength <= item.Loai.length()){
							if(str.equalsIgnoreCase((String)item.Loai.subSequence(0, textlength))){
								array_sort.add(new StringInPair(item.Loai, item.Ma));								
							}
						}
					}
					listview.setAdapter(new MyArrayAdapter(mcontext, array_sort,"NguyenLieu"));				
										
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,int after) {
										
				}
				
				@Override
				public void afterTextChanged(Editable s) {				
					
				}
			});
	        
	    	listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> list, View view,
						int position, long id) {
					StringInPair item = (StringInPair) list
					.getItemAtPosition(position);
					layoutmain.removeAllViews();
					ListChonMonAn listmonantest = new ListChonMonAn(mcontext,item.Ma,item.Loai,"MaNguyenLieu");					
//					Intent intent = new Intent( mcontext, ListMonAnTest.class);
//					Bundle bundle = new Bundle();
//					bundle.putString("Column", "MaNguyenLieu");
//					bundle.putString("Loai", item.Loai);
//					bundle.putInt("Ma", item.Ma);
//					intent.putExtras(bundle);
//					mcontext.startActivity(intent);
					
					layoutmain.addView(listmonantest.getView());
					

				}

			}); 
	    	view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));
			return view;
	}

}
